package com.spring.config.system.initializer;


import com.spring.config.system.config.DatabaseConfig;
import com.spring.config.system.config.MvcConfig;
import com.spring.config.system.config.SpringConfig;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;
import org.thymeleaf.spring4.expression.Mvc;

import javax.servlet.*;
import java.util.EnumSet;

/**
 * ClassName: WebApplicationInitializer2
 * Description:
 * Date: 2017/5/10 23:14
 *
 * @author SAM SHO
 * @version V1.0
 */
public class WebApplicationInitializer2 implements org.springframework.web.WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        /*AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(SpringConfig.class, DatabaseConfig.class, MvcConfig.class);
        ctx.setServletContext(servletContext);

        // 添加 Spring Listener
        servletContext.addListener(new ContextLoaderListener(ctx));

        // 添加 CharacterEncodingFilter
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("utf-8");
        FilterRegistration.Dynamic registration = servletContext.addFilter("characterEncodingFilter", characterEncodingFilter);
        registration.setAsyncSupported(isAsyncSupported());
        registration.addMappingForUrlPatterns(getDispatcherTypes(), false, "*//**//*");

        // 配置spring mvc,这边可以配置servlet的配置
        ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.setAsyncSupported(true);
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);*/
    }

    private boolean isAsyncSupported() {
        return true;
    }

    private EnumSet<DispatcherType> getDispatcherTypes() {
        return isAsyncSupported() ?
                EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ASYNC) :
                EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE);
    }
}
